package com.medical.acc.web.controller;

import com.medical.common.JsonResult;
import com.medical.acc.model.Account;
import com.medical.acc.service.AccountService;
import com.medical.acc.service.PeopleService;
import com.medical.acc.web.viewmodel.AccountModel;
import com.medical.sys.model.User;
import com.medical.sys.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2016/10/12.
 */
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private UserService userService;
    @Autowired
    private PeopleService peopleService;

    //返回全部数据的条数
    @ApiOperation(value="返回全部数据的条数", notes="返回全部数据的条数")
    @RequestMapping(value = "",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getAll(){
        List<Account> list=accountService.selectAll(true);
        return new JsonResult("200","",list.size());
    }
    /**
     *
     * @param query
     * @param page
     * @param pageSize
     * @param sortColumn
     * @param sortDir
     * @return
     */
    //返回携带分页信息的account的总信息json数组
    @ApiOperation(value="返回account的总信息数组", notes="返回account的总信息数组")
    @RequestMapping(value="/getaccount",method = RequestMethod.GET)

    public JsonResult selectAllPeopleAjax(@RequestParam(value = "query" ,required = false) String query, @RequestParam(value = "page" ,required = false) Integer page,
                                          @RequestParam(value = "pageSize" ,required = false) Integer pageSize, @RequestParam(value = "sortColumn" ,required = false) String sortColumn,
                                          @RequestParam(value = "sortDir" ,required = false) String sortDir){

        return new JsonResult(accountService.searchList(query, page, pageSize, sortColumn, sortDir));
    }
    //根据id获取对象
    @ApiOperation(value="根据id获取对象", notes="根据id获取对象")
    @RequestMapping(value = "/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getAccountById (@PathVariable String id){
        AccountModel accountModel=new AccountModel();
        try {

            Account account=accountService.selectByPrimaryKey(id);
            User user=userService.selectByPrimaryKey(account.getUserId());
            accountModel.setId(id);
            accountModel.setUserId(user.getId());
            accountModel.setLevelId(account.getLevelId());
            accountModel.setPayPassword(account.getPayPassword());
            accountModel.setUsername(user.getUsername());
            accountModel.setPassword(user.getPassword());
            accountModel.setEmail(user.getEmail());
            accountModel.setFullname(user.getFullname());
            accountModel.setPhone(user.getPhone());
            return new JsonResult("200", "",accountModel);
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
    }

    /**
     *
     * @param organizationId
     * @param username
     * @param password
     * @param email
     * @param phone
     * @param fullname
     * @param payPassword
     * @param levelId
     * @param isNew
     * @return
     */

    //新增

    //可以保存account（id，userid，levelid），user（id，username，password。email），
    // 如果isnew为true，
    // people（id，uesrid，fullname，phone，inputdate），
    // peopleOrganizationMap（peopleid，organizationId）
    @ApiOperation(value="新增", notes="新增")
    @RequestMapping(value = "/postaccount",method = RequestMethod.POST)
    public JsonResult postaccount(@RequestParam(value = "organizationId",required = false) String organizationId,
                                  @RequestParam(value = "username",required = false) String username,
                                  @RequestParam(value = "password",required = false) String password,
                                  @RequestParam(value = "email",required = false) String email,
                                  @RequestParam(value = "phone",required = false) String phone,
                                  @RequestParam(value = "fullname",required = false) String fullname,
                                  @RequestParam(value = "payPassword",required = false) String payPassword,
                                  @RequestParam(value = "levelId",required = false) int levelId,
                                  @RequestParam(value = "isNew",required = false) Boolean isNew){
        AccountModel accountModel =new AccountModel();
        accountModel.setOrganizationId(organizationId);
        accountModel.setUsername(username);
        accountModel.setPassword(password);
        accountModel.setEmail(email);
        accountModel.setPhone(phone);
        accountModel.setFullname(fullname);
        accountModel.setPayPassword(payPassword);
        accountModel.setLevelId(levelId);
        accountModel.setNew(isNew);
        try{
        accountService.postAccount(accountModel);
        return new JsonResult("200","增添成功");
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
        }

    /**
     *
     * @param id
     * @param fullname
     * @param phone
     * @param username
     * @param password
     * @param email
     * @param payPassword
     * @param levelId
     * @return
     */
    //编辑
    @ApiOperation(value="编辑", notes="编辑")
    @RequestMapping(value = "/patchaccount/{id}/{userId}",method = RequestMethod.POST)
    public JsonResult patchAccount(@PathVariable String id,
                                   @PathVariable String userId,
                                   @RequestParam(value = "fullname",required = false) String fullname,
                                   @RequestParam(value = "phone",required = false) String phone,
                                   @RequestParam(value = "username",required = false) String username,
                                   @RequestParam(value = "password",required = false) String password,
                                   @RequestParam(value = "email",required = false) String email,
                                   @RequestParam(value = "payPassword",required = false) String payPassword,
                                   @RequestParam(value = "levelId",required = false) int levelId){
        AccountModel accountModel=new AccountModel();
        accountModel.setId(id);
        accountModel.setUserId(userId);
        accountModel.setLevelId(levelId);
        accountModel.setPayPassword(payPassword);
        accountModel.setUsername(username);
        accountModel.setPassword(password);
        accountModel.setEmail(email);
        accountModel.setFullname(fullname);
        accountModel.setPhone(phone);
        try{
        accountService.patchAccount(accountModel);
        return new JsonResult("200","更新成功");
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
        }

    //删除
    @ApiOperation(value="删除", notes="删除")
    @RequestMapping(value = "/deleteaccount/{id}",method = RequestMethod.POST)
    private JsonResult deleteAccount(@PathVariable String id){
        try{
        accountService.deleteByPrimaryKey(id);
        return new JsonResult("200","删除成功");
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
        }

    /**
     *
     * @param id
     * @param remark
     * @return
     */
    //保存消费记录
    @RequestMapping(value="/patchrecord", method = RequestMethod.POST)
    public JsonResult patchrecord(@RequestParam("id") String id ,
                                 @RequestParam("remark") String remark ){
        try{
        accountService.patchRecordForRemark(id, remark);
        return new JsonResult("200","保存成功");
        }catch (Exception e){

            return new JsonResult("500",e.getMessage());
        }
        }

}
